package com.ltmall.api.parse;

import com.ltmall.api.bean.Order;
import com.ltmall.api.bean.OrderContent;
import com.ltmall.api.bean.ThGroupBuying;
import com.ltmall.api.comm.Path;
import com.ltmall.api.entity.JsonOrder;
import com.ltmall.api.manager.GoodsManager;
import com.ltmall.api.manager.OrderManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class OrderParse {
    //商品图片的路径
    static String goodsPath = Path.nginxServerBusinessDir + Path.GOODS + File.separator;

    //订单列表列表的解析
    public static List<JsonOrder> orderListParse(List<Order> list, OrderManager orderManager,
                                                 GoodsManager goodsManager) {
        List<JsonOrder> jOrderList = new ArrayList<JsonOrder>();

        //【1】遍历订单
        for (Order order : list) {
            int orderID = order.getOrderID();
            JsonOrder jOrder = new JsonOrder();
            List<OrderContent> contentList = orderManager.getOrderContent(orderID);
            //【2】如果订单状态为5的情况下只返回还没有评论的商品
            if (order.getStation() == 5) {//评论订单列表
                //判断有没有评论
                List<OrderContent> unCommentContentList = new ArrayList<OrderContent>();
                for (OrderContent content : contentList) {
                    boolean opTag = orderManager.isComment(orderID, content.getGoodsID(),
                            content.getGoodsFormat(), order.getUserID());
                    if (!opTag) {//表示未评论评价了
                        unCommentContentList.add(content);
                    }
                }
                jOrder.setOrderContent(unCommentContentList);
            } else {
                jOrder.setOrderContent(contentList);
            }
            //【3】增加团购信息
            if (contentList.size() == 1 && contentList.get(0).getGroupID() > 0) {
                ThGroupBuying groupBuying = goodsManager.groupBuyingGet(contentList.get(0)
                        .getGroupID());
                jOrder.setGroupBuying(groupBuying);
            }
            jOrder.setOrder(order);

            jOrderList.add(jOrder);
        }

        return jOrderList;
    }

    //返回商品所有的图片的路径
    public static String getAllGoodsPicture(String allPicture) {
        String goodsPic = "";
        if (allPicture == null) {
            return "";
        }
        String picArr[] = allPicture.split(";");
        for (String pic : picArr) {
            goodsPic += goodsPath + pic + ";";
        }
        int index = goodsPic.lastIndexOf(";");
        return goodsPic.substring(0, index);
    }

    //返回第一张商品图片的
    public static String getOneGoodsPicture(String allPicture) {
        if (allPicture == null) {
            return "";
        }
        String picArr[] = allPicture.split(";");
        return goodsPath + picArr[0];
    }

}
